package repository

import (
	"gitverse.ru/pahtll/checats/src/internal/models"
	_ "gorm.io/driver/postgres"
	_ "gorm.io/gorm"
)

func (r *Repository) GetAllUsers() ([]*models.User, error) {
	var users []*models.User
	err := r.db.Model(&models.User{}).
		Preload("Posts").
		Preload("Commentaries").
		Find(&users).Error

	return users, err
}

func (r *Repository) GetUserById(id int) (*models.User, error) {
	var user *models.User
	err := r.db.Model(&models.User{}).
		Preload("Posts").
		Preload("Commentaries").
		First(&user, id).Error

	return user, err

}

func (r *Repository) GetUserByUsername(username string) (*models.User, error) {
	var user *models.User
	err := r.db.Model(&models.User{}).
		Preload("Posts").
		Preload("Commentaries").
		First(&user, "username = ?", username).Error

	return user, err
}

func (r *Repository) CreateUser(user *models.User) error {
	return r.db.Create(user).Error
}

func (r *Repository) UpdateUser(user *models.User) error {
	return r.db.Save(user).Error
}
